package com.headwind.logistics.logistics.interceptor;

import com.headwind.logistics.common.constant.UserSessionKey;
import com.headwind.logistics.common.pojo.Customer;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.pojo.Transportuser;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 小恶魔
 * @Date 2020-07-20 16:23
 * @Version 1.0
 * @DESCRIPTION 前台登录拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //顾客对象
        Customer customer = (Customer) request.getSession().getAttribute(String.valueOf(UserSessionKey.CUSTOMER));
        //运输人对象
        Transportuser transportUser = (Transportuser) request.getSession().getAttribute(String.valueOf(UserSessionKey.TRANSPORTUSER));

        //如果session中没有user，表示没登陆
        if (customer == null && transportUser ==null){
            //这个方法返回false表示忽略当前请求，如果一个用户调用了需要登陆才能使用的接口，如果他没有登陆这里会直接忽略掉
            //当然你可以利用response给用户返回一些提示信息，告诉他没登陆
            response.sendRedirect(request.getContextPath()+"/toLogin");
            return false;
        }else {
            return true;    //如果session里有user，表示该用户已经登陆，放行，用户即可继续调用自己需要的接口
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
